package com.hongkzh.www.conmon;

import cn.jiguang.common.ClientConfig;
import cn.jiguang.common.resp.APIConnectionException;
import cn.jiguang.common.resp.APIRequestException;
import cn.jpush.api.JPushClient;
import cn.jpush.api.push.PushResult;
import cn.jpush.api.push.model.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.notification.AndroidNotification;
import cn.jpush.api.push.model.notification.IosNotification;
import cn.jpush.api.push.model.notification.Notification;
import com.google.gson.JsonObject;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class PushMessage {

    protected static final Logger LOG = LoggerFactory.getLogger(PushMessage.class);
    protected static   String APP_KEY ;
    protected static   String MASTER_SECRET ;
    protected static final ClientConfig clientConfig = ClientConfig.getInstance();
     static JPushClient jpushClient;

    public static void main(String[] args) {
        PropKit.use("a_little_config.txt");
        String cid= "1";
        JsonObject jsonExtra = new JsonObject();
        jsonExtra.addProperty("extra1", 1);
        jsonExtra.addProperty("extra2", false);
        jsonExtra.addProperty("extra3", 3);
        jsonExtra.addProperty("extra4", 4);
        jsonExtra.addProperty("extra5", 5);
        jsonExtra.addProperty("extra6", 6);
        sendPush(cid,jsonExtra.toString(),"xxx点赞了你");
    }
    public static void sendPush(String uid,String json,String title) {
        Record r= Db.findFirst(Db.getSqlPara("user.getUserRegistrationID",uid));
       if(null!=r) {
           APP_KEY = PropKit.get("j.appkey");
           MASTER_SECRET = PropKit.get("j.appSecret");
           jpushClient = new JPushClient(MASTER_SECRET, APP_KEY, null, clientConfig);
           final PushPayload payload = buildPushObject_with_extra(r.getStr("cid"), json, r.getStr("name") + title);
           try {
               PushResult result = jpushClient.sendPush(payload);
               LOG.info("Got result - " + result);
               //System.out.println(result);

           } catch (APIConnectionException e) {
               LOG.error("Connection error. Should retry later. ", e);
               LOG.error("Sendno: " + payload.getSendno());

           } catch (APIRequestException e) {
               LOG.error("Error response from JPush server. Should review and fix it. ", e);
               LOG.info("HTTP Status: " + e.getStatus());
               LOG.info("Error Code: " + e.getErrorCode());
               LOG.info("Error Message: " + e.getErrorMessage());
               LOG.info("Msg ID: " + e.getMsgId());
               LOG.error("Sendno: " + payload.getSendno());
           }
       }
    }
    public static PushPayload buildPushObject_with_extra(String cid,String json,String title) {

        return PushPayload.newBuilder()
                .setPlatform(Platform.android_ios())
                .setAudience(Audience.registrationId(cid))
                .setNotification(Notification.newBuilder()
                        .setAlert("有新的消息")
                        .addPlatformNotification(AndroidNotification.newBuilder()
                                .setTitle(title)
                                .addExtra("json", json)
                                .build())
                        .addPlatformNotification(IosNotification.newBuilder()
                                .incrBadge(1)
                                .addExtra("json", json).build())
                        .build())
                .build();


    }
}
